﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess.Operate
{
    public class DA_Project_ProjectCommission_Inherit:SqlInteractionBase
    {
        public DataEntity.T_Project_ProjectCommission FindByProjectIDAndAgentID(string id,string agentID)
        {
            DataEntity.T_Project_ProjectCommission Project_ProjectCommission = new DataEntity.T_Project_ProjectCommission();
            string sSql = "select * from t_Project_ProjectCommission where Project_ProjectCommission_ProjectID='" + id + "' and Project_ProjectCommission_AgentID="+agentID;
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable toReturn = new DataTable();
            cmd.CommandText = sSql;
            cmd.CommandType = CommandType.Text;


            try
            {
                cmd.Connection = _mainConnection;
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmd.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    Project_ProjectCommission.Project_ProjectCommission_ID = (Guid)toReturn.Rows[0]["Project_ProjectCommission_ID"];
                    //Project_ProjectCommission.Project_ProjectCommission_Date = toReturn.Rows[0]["Project_ProjectCommission_Date"] == null ?  : (DateTime)toReturn.Rows[0]["Project_ProjectCommission_Date"];
                    Project_ProjectCommission.Project_ProjectCommission_AgentID = (int)toReturn.Rows[0]["Project_ProjectCommission_AgentID"];
                    Project_ProjectCommission.Project_ProjectCommission_ProjectID = (Guid)toReturn.Rows[0]["Project_ProjectCommission_ProjectID"];
                    Project_ProjectCommission.Project_ProjectCommission_Status = (int)toReturn.Rows[0]["Project_ProjectCommission_Status"];
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    _mainConnection.Close();
                }

                cmd.Dispose();

                adapter.Dispose();
            }
            return Project_ProjectCommission;

        }

        public void GetValue(string projectID, string AgencyType, out object obj1, out object obj2, out object obj3, out object obj4)
        {
            SqlConnection conn = _mainConnection;
            SqlCommand command = new SqlCommand("SP_count", conn);
            command.CommandType = CommandType.StoredProcedure;
            SqlParameter para1 = new SqlParameter("@para_AgencyType", AgencyType);
            SqlParameter para2 = new SqlParameter("@para_ProjectID", projectID);
            SqlParameter para3 = new SqlParameter("@total_turnover",SqlDbType.Int);
            SqlParameter para4 = new SqlParameter("@total_value",SqlDbType.Decimal);
            SqlParameter para5 = new SqlParameter("@expect_commissions",SqlDbType.Decimal);
            SqlParameter para6 = new SqlParameter("@received_commission", SqlDbType.Decimal);
            para1.Direction = ParameterDirection.Input;
            para2.Direction = ParameterDirection.Input;
            para3.Direction = ParameterDirection.Output;
            para4.Direction = ParameterDirection.Output;
            para5.Direction = ParameterDirection.Output;
            para6.Direction = ParameterDirection.Output;
            command.Parameters.Add(para1);
            command.Parameters.Add(para2);
            command.Parameters.Add(para3);
            command.Parameters.Add(para4);
            command.Parameters.Add(para5);
            command.Parameters.Add(para6);
            conn.Open();
            command.ExecuteNonQuery();
             obj1 = para3.Value;
             obj2 = para4.Value;
             obj3 = para5.Value;
             obj4 = para6.Value;
            conn.Close();
           
        }



       

    }
}
